package com.xianbeiying.app.ckgl.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.loopj.android.http.TextHttpResponseHandler;
import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.activity.CounselDetailActivity;
import com.xianbeiying.app.ckgl.adapter.CounselListdapter;
import com.xianbeiying.app.ckgl.bean.UrlBean;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.http.HttpPostManger;
import com.xianbeiying.app.ckgl.model.GetModel;
import com.xianbeiying.app.ckgl.util.LogUtils;
import com.xianbeiying.app.ckgl.widght.NoScrollListView;

import cz.msebera.android.httpclient.Header;

/**
 * 咨询
 */
public class CounselFragment  extends BaseFragment implements HttpCallBackUtil,View.OnClickListener{
    private static CounselFragment counselFragment;
    public static CounselFragment getInstance(){
        counselFragment=new CounselFragment();
        return counselFragment;
    }
    public static WebView webView;
    private View view;
    private GetModel getModel;
    private ImageView iv_title_left;
    private TextView tv_title;
    private NoScrollListView counsel_list;
    private CounselListdapter counselListdapter;
    //分类
    private TextView tv_rmzx,tv_ynzx,tv_dfdt,tv_gjzx;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_counsel,null);
        initView();
        return view;
    }

    private void initView() {
        getModel=new GetModel(getActivity());
        getModel.addResponseListener(this);
        tv_title=(TextView) view.findViewById(R.id.tv_title);
        iv_title_left=(ImageView) view.findViewById(R.id.iv_title_left);
        counsel_list=(NoScrollListView) view.findViewById(R.id.counsel_list);
        tv_rmzx=(TextView) view.findViewById(R.id.tv_rmzx);
        tv_ynzx=(TextView) view.findViewById(R.id.tv_ynzx);
        tv_dfdt=(TextView) view.findViewById(R.id.tv_dfdt);
        tv_gjzx=(TextView) view.findViewById(R.id.tv_gjzx);
        webView=(WebView) view.findViewById(R.id.webView);
        tv_rmzx.setSelected(true);
        iv_title_left.setVisibility(View.GONE);
        initData();
        initEvent();
    }
    private void initData() {
        tv_title.setText("资讯");
        counselListdapter=new CounselListdapter(getActivity());
        counsel_list.setAdapter(counselListdapter);
        counsel_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent().setClass(getActivity(), CounselDetailActivity.class));
            }
        });

//        getModel.GETInformation("news",true);

//        String url4load = "http://192.168.1.102:8080/CloudIIMS/notice/news";
//        CookieSyncManager.createInstance(getActivity());
//        CookieManager cookieManager = CookieManager.getInstance();
//        Cookie sessionCookie = MyApplication.appCookie;
//        if (sessionCookie != null) {
//            String cookieString = sessionCookie.getName() + "=" + sessionCookie.getValue() + "; domain=" +            sessionCookie.getDomain();
//            cookieManager.setCookie(url4load, cookieString);
//            CookieSyncManager.getInstance().sync();
//            webView.loadUrl(cookieString);
//        }
//        LogUtils.e("http://"+ MyApplication.IP+":8080/CloudIIMS/notice/news");
//        webView.loadUrl("http://"+ MyApplication.IP+UrlBean.GETZIXUN + "news");
        HttpPostManger.get("http://"+ MyApplication.IP+ UrlBean.GETZIXUN + "news",new TextHttpResponseHandler() {
            //获取成功
            @Override
            public void onSuccess(int i, Header[] headers, String s){
                LogUtils.e("咨询","加载成功"+i+"======");
//                webView.loadDataWithBaseURL(null, s, "text/html",
//                        "utf-8", null);
                webView.getSettings().setDomStorageEnabled(true);//有可能是DOM储存API没有打开
                webView.getSettings().setBlockNetworkImage(false);
                webView.getSettings().setUseWideViewPort(false);
                webView.getSettings().setJavaScriptEnabled(true);
                webView.getSettings().setAllowFileAccess(true);
                webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
                webView.getSettings().setLoadsImagesAutomatically(true);////支持自动加载图片
                webView.loadDataWithBaseURL("http://"+ MyApplication.IP+":8080/", s, "text/html", "utf-8", null);
//                WebSettings webSettings = webView.getSettings();
//                //设置WebView属性，能够执行Javascript脚本
//                webSettings.setJavaScriptEnabled(true);
//                //设置可以访问文件
//                webSettings.setAllowFileAccess(true);
//                //设置支持缩放
//                webSettings.setBuiltInZoomControls(true);
//                //加载需要显示的网页
//                webView.loadUrl(s);
//                //设置Web视图
//                webView.setWebViewClient(new webViewClient ());
                LogUtils.e("咨询","成功"+s+"======");
                webView.setWebViewClient(new WebViewClient(){
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        // TODO Auto-generated method stub
                        LogUtils.e("ppppppp",url);
                        //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
//                        view.loadDataWithBaseURL("http://"+ MyApplication.IP+":8080/",url, "text/html", "utf-8", null);
                        getContent(url,view);
                        return true;
                    }
                });
            }

            //获取失败
            @Override
            public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                Log.e("咨询", "加载失败");
            }
        });
    }
    private void initEvent() {
        iv_title_left.setOnClickListener(this);
        tv_rmzx.setOnClickListener(this);
        tv_ynzx.setOnClickListener(this);
        tv_dfdt.setOnClickListener(this);
        tv_gjzx.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onCallBackData(String url, Object content) {
         if(url.equals("资讯信息")){
             if(content!=null){

             }
         }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_title_left:
                getActivity().finish();
                break;
            //热门资讯
            case R.id.tv_rmzx:
                showDefult();
                tv_rmzx.setSelected(true);
                break;
            //业内资讯
            case R.id.tv_ynzx:
                showDefult();
                tv_ynzx.setSelected(true);
                break;
            //地方动态
            case R.id.tv_dfdt:
                showDefult();
                tv_dfdt.setSelected(true);
                break;
            //国际资讯
            case R.id.tv_gjzx:
                showDefult();
                tv_gjzx.setSelected(true);
                break;

        }
    }
    //设置默认背景
    private void showDefult(){
        tv_rmzx.setSelected(false);
        tv_ynzx.setSelected(false);
        tv_dfdt.setSelected(false);
        tv_gjzx.setSelected(false);

    }
    private  void getContent(String url,final WebView mmwebView){
        HttpPostManger.get(url,new TextHttpResponseHandler() {
            //获取成功
            @Override
            public void onSuccess(int i, Header[] headers, String s){
                LogUtils.e("咨询","加载成功"+i+"======");
//                webView.loadDataWithBaseURL(null, s, "text/html",
//                        "utf-8", null);
                mmwebView.getSettings().setDomStorageEnabled(true);//有可能是DOM储存API没有打开
                mmwebView.getSettings().setBlockNetworkImage(false);
                mmwebView.getSettings().setUseWideViewPort(false);
                mmwebView.getSettings().setJavaScriptEnabled(true);
                mmwebView.getSettings().setAllowFileAccess(true);
                mmwebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
                mmwebView.getSettings().setLoadsImagesAutomatically(true);////支持自动加载图片
                mmwebView.loadDataWithBaseURL("http://"+ MyApplication.IP+":8080/", s, "text/html", "utf-8", null);
//                WebSettings webSettings = webView.getSettings();
//                //设置WebView属性，能够执行Javascript脚本
//                webSettings.setJavaScriptEnabled(true);
//                //设置可以访问文件
//                webSettings.setAllowFileAccess(true);
//                //设置支持缩放
//                webSettings.setBuiltInZoomControls(true);
//                //加载需要显示的网页
//                webView.loadUrl(s);
//                //设置Web视图
//                webView.setWebViewClient(new webViewClient ());
                LogUtils.e("咨询","成功"+s+"======");
            }

            //获取失败
            @Override
            public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                Log.e("咨询", "加载失败");
            }
        });
    }


}
